﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace YmtAuth.Common.Extend
{
    public static class String_Extend
    {
        public static bool IsMail(this string val, string pattern = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
        {
            if (string.IsNullOrEmpty(val)) return false;
            if (string.IsNullOrEmpty(pattern))
                pattern = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";

            return Regex.IsMatch(val, pattern, RegexOptions.IgnoreCase);
        }
        public static bool _IsMobile(this string val, string pattern = @"^[1]+[3,4,5,7,8]+\d{9}")
        {
            if (string.IsNullOrEmpty(val) || val.Length != 11) return false;
            if (string.IsNullOrEmpty(pattern))
                pattern = @"^[1]+[3,4,5,7,8]+\d{9}";
            return Regex.IsMatch(val, pattern, RegexOptions.IgnoreCase);
        }

        public static byte[] GetBytes(this string val, string encoding = "utf-8")
        {
            if (string.IsNullOrEmpty(val)) return new byte[0];
            return Encoding.GetEncoding(encoding).GetBytes(val);
        }

        public static string[] GetAppSettingValus(this string key, string defaultVal,char split=',')
        {
            if (string.IsNullOrEmpty(key)) throw new ArgumentNullException("key can't null");
            var value = ConfigurationManager.AppSettings[key];
            if (string.IsNullOrEmpty(value))
            {
                return defaultVal.Split(new char[] {split});
            }
            return value.Split(new char[] {split});
        }
    }
}
